package com.atguigu.java2;

import com.atguigu.java1.Person;
import org.junit.Test;

import java.lang.reflect.Field;
import java.lang.reflect.Modifier;

/**
 * 获取运行时类的属性结构
 */
public class FieldTest {
    @Test
    public void test1() {
        Class clazz = Person.class;

        //获取属性结构
        // getFields:获取当前运行时类及父类中的public访问权限的属性
        Field[] fields = clazz.getFields();
        for (Field f : fields) {
            System.out.println(f);
        }
        System.out.println();

        // 获取当前运行时类中声明的所有属性，不包含父类中声明的属性
        Field[] dealaredFields = clazz.getDeclaredFields();
        for (Field f : dealaredFields) {
            System.out.println(f);
        }
    }

    @Test
    public void test2() throws Exception{
        Class clazz = Person.class;
        System.out.println(clazz.getDeclaredField("name"));
        Field[] dealaredFields = clazz.getDeclaredFields();
        for (Field f : dealaredFields) {
            // 1.权限修饰符
            int modifier = f.getModifiers();
            System.out.println("权限修饰符" + Modifier.toString(modifier));

            // 2. 数据类型
            Class type = f.getType();
            System.out.println("数据类型：" + type.toString());

            // 3. 变量名
            String fName = f.getName();
            System.out.println("变量名" + fName);
            System.out.println(f);
        }
    }
}
